package com.ruoyi.consulting.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consulting.domain.XlkjConsulting;
import com.ruoyi.consulting.service.IXlkjConsultingService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题咨询Controller
 *
 * @author wang
 * @date 2022-01-15
 */
@RestController
@RequestMapping("/h5/consulting/consulting")
public class XlkjConsultingControllerH5 extends BaseController
{
    @Autowired
    private IXlkjConsultingService xlkjConsultingService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询问题咨询列表
     */
//    @PreAuthorize("@ss.hasPermi('consulting:consulting:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjConsulting xlkjConsulting)
    {
        //筛选答复完毕的（自定义状态）
        if(xlkjConsulting.getPhone() == null){
            xlkjConsulting.setType(5);
        }
        startPage();
        List<XlkjConsulting> list = xlkjConsultingService.selectXlkjConsultingList(xlkjConsulting);
        for(XlkjConsulting consulting:list) {
            consulting.setUserName(sysUserService.selectUserById(consulting.getUserId()).getUserName());
        }
        return getDataTable(list);
    }

    /**
     * 导出问题咨询列表
     */
//    @PreAuthorize("@ss.hasPermi('consulting:consulting:export')")
//    @Log(title = "问题咨询", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjConsulting xlkjConsulting)
    {
        List<XlkjConsulting> list = xlkjConsultingService.selectXlkjConsultingList(xlkjConsulting);
        ExcelUtil<XlkjConsulting> util = new ExcelUtil<XlkjConsulting>(XlkjConsulting.class);
        return util.exportExcel(list, "问题咨询数据");
    }

    /**
     * 获取问题咨询详细信息
     */
//    @PreAuthorize("@ss.hasPermi('consulting:consulting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(xlkjConsultingService.selectXlkjConsultingById(id));
    }

    /**
     * 新增问题咨询
     */
//    @PreAuthorize("@ss.hasPermi('consulting:consulting:add')")
//    @Log(title = "问题咨询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjConsulting xlkjConsulting)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(xlkjConsulting.getPhone());
        xlkjConsulting.setUserId(sysUser.getUserId());
        return toAjax(xlkjConsultingService.insertXlkjConsulting(xlkjConsulting));
    }

    /**
     * 修改问题咨询
     */
    @PreAuthorize("@ss.hasPermi('consulting:consulting:edit')")
    @Log(title = "问题咨询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjConsulting xlkjConsulting)
    {
        return toAjax(xlkjConsultingService.updateXlkjConsulting(xlkjConsulting));
    }

    /**
     * 删除问题咨询
     */
    @PreAuthorize("@ss.hasPermi('consulting:consulting:remove')")
    @Log(title = "问题咨询", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjConsultingService.deleteXlkjConsultingByIds(ids));
    }
}
